#include "tcpsocket.hpp"

int main(int argc,char *argv[])
{
    //客户端虽然可以不用主动绑定地址，但是必须知道服务端的地址
    if (argc < 3) {
        std::cout << "usage: arg error\n";
        std::cout << "\t ./tcp_cli 192.168.2.2 9000\n";
        return -1;
    }
    int srv_port = std::stoi(argv[2]);
    std::string srv_ip = argv[1];

    TcpSocket sock;
    //创建套接字
    CHECK_RET(sock.Socket());
    //向服务端发起连接请求
    CHECK_RET(sock.Connect(srv_ip, srv_port));
    //循环的收发数据
    while(1) {
        struct cal_t cal;
        std::cin >> cal.num1 >> cal.num2 >> cal.op;
        for (int i = 0; i < sizeof(cal); i++) {
            printf("%02x", *((char*)&cal + i));
        }
        printf("\n");
        std::string data;
        data.assign((char*)&cal, sizeof(struct cal_t));
        CHECK_RET(sock.Send(data));

        data.clear();
        CHECK_RET(sock.Recv(&data));
        std::cout << "server response:" << data << std::endl;
    }
    //关闭套接字
    sock.Close();
    return 0;
}
